package com.flutter_ecg.flutter_ecg.ecgapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.flutter_ecg.flutter_ecg.ecgapplication.ecgdevicescan.ECGDeviceScanActivity;
import com.flutter_ecg.flutter_ecg.ecgapplication.ecgrealtimecheck.EcgCheckActivity;
import com.flutter_ecg.flutter_ecg.ecgapplication.utils.Constants;

import java.io.Serializable;

import static com.flutter_ecg.flutter_ecg.ecgapplication.utils.Constants.ECGDEVICEID_KEY_NAME;
import static com.flutter_ecg.flutter_ecg.ecgapplication.utils.Constants.SP_ECGDEVICEID_NAME;

public class IKEcgCheckController {
    private static volatile IKEcgCheckController instance = null;
    public static synchronized IKEcgCheckController getInstance() {
        if (instance == null) {
            synchronized (IKEcgCheckController.class) {
                if (instance == null) {
                    instance = new IKEcgCheckController();
                }
            }
        }
        return instance;
    }

    public static synchronized void showEcgView(Context context, IKEcgCheckInterface ikEcgCheckInterface, String customData) {
        Intent intent = new Intent();
        intent.putExtra(Constants.IKECGINTERFACE_KEY, ikEcgCheckInterface);
        intent.putExtra(Constants.CUSTOMDATA_KEY, customData);
        if (hasBondDevice(context)) {
            intent.setClass(context, EcgCheckActivity.class);
        } else {
            intent.setClass(context, ECGDeviceScanActivity.class);
        }
        context.startActivity(intent);
    }

    public static boolean hasBondDevice(Context context) {
        String deviceid = "";
        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_ECGDEVICEID_NAME, Context.MODE_PRIVATE);
        deviceid = sharedPreferences.getString(ECGDEVICEID_KEY_NAME, "");
        return !TextUtils.isEmpty(deviceid);
    }
}
